package org.linlinjava.litemall.admin.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.task.ScheduledCronTaskRegistrar;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallScheduledJob;
import org.linlinjava.litemall.db.service.LitemallScheduledJobService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminScheduledJobService {
    private final LitemallScheduledJobService scheduledJobService;
    private final ScheduledCronTaskRegistrar scheduledCronTaskRegistrar;

    public ResponseUtil.Response scheduledJobs(Integer limit, Integer page, Integer type, Integer status, String name) {
        IPage<LitemallScheduledJob> scheduledJobs = scheduledJobService.page(new Page<>(page,limit),Wrappers.lambdaQuery(LitemallScheduledJob.class)
                .eq(type != null, LitemallScheduledJob::getType, type)
                .eq(status != null && status != 3, LitemallScheduledJob::getStatus, status)
                .eq(status != null && status != 3, LitemallScheduledJob::getIsDie, 1)
                .eq(status != null && status == 3, LitemallScheduledJob::getIsDie, 0)
                .like(StrUtil.isNotBlank(name), LitemallScheduledJob::getName, name)
                .orderByDesc(LitemallScheduledJob::getId));
        return ResponseUtil.okPage(scheduledJobs);
    }

    public ResponseUtil.Response openOrclose(Integer id) {
        LitemallScheduledJob scheduledJob = scheduledJobService.getById(id);
        scheduledJob.setStatus(!scheduledJob.getStatus());
        //开启任务
        if (scheduledJob.getStatus()) {
            try {
                scheduledCronTaskRegistrar.schedulerAdd(scheduledJob, Class.forName(scheduledJob.getClassName()));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return ResponseUtil.fail("调用目标错误!");
            }
        } else {
            scheduledCronTaskRegistrar.schedulerDelete(scheduledJob.getId().toString());
        }
        return ResponseUtil.ok(scheduledJobService.updateById(scheduledJob));
    }

    public ResponseUtil.Response deleted(Integer id) {
        return ResponseUtil.ok(scheduledJobService.removeById(id));
    }

    public ResponseUtil.Response batchDeleted(List<Integer> ids) {
        return ResponseUtil.ok(scheduledJobService.removeBatchByIds(ids));
    }

    public ResponseUtil.Response abortMission(Integer id) {
        boolean update = scheduledJobService.update(Wrappers.lambdaUpdate(LitemallScheduledJob.class)
                .eq(LitemallScheduledJob::getId, id)
                .set(LitemallScheduledJob::getIsDie, 0));
        scheduledCronTaskRegistrar.schedulerDelete(id.toString());
        return update ? ResponseUtil.ok() : ResponseUtil.fail();
    }

    public ResponseUtil.Response triggerJob(Integer id) {
        return scheduledCronTaskRegistrar.schedulerTriggerJob(id.toString());
    }

    public ResponseUtil.Response rescheduleJob(LitemallScheduledJob scheduledJob) {
        if (ObjectUtil.isNull(scheduledJob)) {
            return ResponseUtil.badArgument();
        }
        if (scheduledCronTaskRegistrar.checkExists(scheduledJob)) {
            scheduledCronTaskRegistrar.schedulerDelete(scheduledJob.getId().toString());
            try {
                scheduledCronTaskRegistrar.schedulerAdd(scheduledJob, Class.forName(scheduledJob.getClassName()));
            } catch (ClassNotFoundException e) {
                return ResponseUtil.fail("任务更新失败！");
            }
        }
        return ResponseUtil.ok();
    }
}
